package com.pps.sdk.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import android.os.Environment;
import android.util.Log;

public class ConfigHandler {
	private static boolean sdIsExist = true;
	private static Properties properties = null;
	static {
		sdIsExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在;
		if (sdIsExist) {
			File sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
			sdDir = new File(sdDir.getPath() + "/ppsgame");

			if (!sdDir.exists()) {
				sdDir.mkdirs();
			}
			
			String configPath = sdDir.getPath() + "/config.properties";
			File configFile = new File(configPath);
			try {
				if(!configFile.exists()){
					configFile.createNewFile();
				}
				
				InputStream in = new FileInputStream(configFile);
				properties = new Properties();
				properties.load(in);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 检测当前设备是否是测试设备
	 * @return
	 */
	public static boolean isTestPhone(){
		if(properties == null){
			return false;
		}
		String result = properties.getProperty("tester", "false");
		Log.d("Sqlite", "isTestPhone => " + result);
		return Boolean.parseBoolean(result);
	}
	
	/**
	 * 设置当前手机是否为测试设备
	 * @param test
	 */
	public static void setTestPhone(boolean test){
		if(properties.containsKey("tester")){
			properties.setProperty("tester", String.valueOf(test));
		}else{
			properties.put("tester", String.valueOf(test));
		}
		
		String savePath = Environment.getExternalStorageDirectory() + "/ppsgame/config.properties";
		try {
			properties.store(new FileOutputStream(savePath), "");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
